<template>
  <div class="wrapper">
    <div class="title-wrapper">
      <div class="bigTitle">成本分析</div>
    </div>
    <div style="text-align: right">
      <time-picker class="cus-wrapper" ref="timeRef" @change="handleChange"/>
    </div>
    <cast-bar :source="source"/>
  </div>
</template>

<script>
import {costBankTrade} from "@/api/services/dashboard.service";
import castBar from './cast-bar'
import timePicker from './time-picker'
import moment from "moment";


export default {
  name: "balance",
  components: {castBar, timePicker},
  data() {
    return {
      source: [],
      currentTime: [],
      stime: '',
      etime: ''
    }
  },
  mounted() {
    this.$nextTick(() => {
      let params = this.$refs.timeRef.initTime()
      this.getTime(params)
      this.getData()
    })
  },
  methods: {
    _paddZero(data) {
      return data < 10 ? '0' + data : data
    },
    handleChange(data) {
      if (!data) return
      this.getTime(data)
      this.getData()
    },
    getTime(data) {
      const end = moment(`${data.year}-${data.moment}`, "YYYY-MM").daysInMonth();
      this.stime = `${data.year}-${this._paddZero(data.moment)}-01 00:00:00`
      this.etime = `${data.year}-${this._paddZero(data.moment)}-${end} 23:59:59`
    },
    getData() {
      costBankTrade({stime: this.stime, etime: this.etime}).then(res => {
        const {data} = res
        this.source = data.map(item => ({...item, value: item.result, name: item.typeName}))
      })
    }
  }
}
</script>


<style lang="less">
.cus-wrapper {
  .el-select.selectTime {
    width: 70px !important;
  }
}
</style>
<style lang="less" scoped>
.title-wrapper {
  display: flex;
  justify-content: space-between;

  .bigTitle {
    border-bottom: none;
    padding: 8px 0;
    color: #3A71A8;
    font-size: 18px;
    font-weight: bold;
    white-space: nowrap;
  }
}

.wrapper {
  width: 100%;
}
</style>
